#!/bin/bash
if [ -d "$HOME/Miao-Yunzai" ]; then
    lsy="$HOME/Miao-Yunzai"
    jh="$HOME/Miao-Yunzai/plugins"
    xrk="$HOME/xrk"
elif [ -d "$HOME/Yunzai-Bot" ]; then
    lsy="$HOME/Yunzai-Bot"
    jh="$HOME/Yunzai-Bot/plugins"
    xrk="$HOME/xrk"
elif [ -d "$HOME/yxybot" ]; then
    lsy="$HOME/yxybot"
    jh="$HOME/yxybot/plugins"
    xrk="$HOME/xrk"
else
    echo "您未安装机器人,记得通过脚本安装机器人哦"
fi

if ! [ -d "$jh/xiaoyue-plugin" ]; then
    echo "检测到您未安装小月插件，正在安装小月插件"
    cd $lsy
    git clone --depth=1 https://gitee.com/yunxiyuan/xiaoyue-plugin.git ./plugins/xiaoyue-plugin
    cd ~
fi
if ! [ -d "$jh/xitian-plugin" ]; then
    echo "检测到您未安装戏天插件，正在安装戏天插件"
    cd $lsy
    git clone https://gitee.com/XiTianGame/xitian-plugin.git ./plugins/xitian-plugin/
    cd ~
fi

if [ -d "$lsy" ]; then
    cd $lsy
echo -e "\e[1;32m脚本使用须知\e[0m"
echo "---------------------------------"
echo -e "\e[36m插件很眼花缭乱，如果想了解插件功能请访问：\e[0m"
echo -e "\e[36mhttps://gitee.com/yhArcadia/Yunzai-Bot-plugins-index/\e[0m"
echo -e "\e[36m如果想更轻松的下载插件，请对机器人说\e[1;35m#小月帮助\e[0m\e[36m了解命令详情\e[0m"
echo -e "\e[36m如果想要查看 js 插件的功能，请对机器人说\e[1;35m#插件列表(戏天)\e[0m\e[36m，等待响应\e[0m"
sleep 3
    sleep 3
    echo "----------------------------------"
    echo -e "\\e[34m 欢迎使用向日葵插件菜单 \\e[0m"
    echo "----------------------------------"
    echo "1. 白狐脚本菜单(超级全，不会用的自行Ctrl＋c退出)"
    echo "2. 一建安装所有插件包(不含py)"
    echo "3. 安装python(仅限Ubuntu22.04)"
    echo "4. 安装喵喵插件"
    echo "5. 安装逍遥插件"
    echo "6. 安装抽卡插件"
    echo "7. 安装千羽插件(仅Yunzai-Bot)"
    echo "8. 安装椰奶插件"
    echo "9. 安装锅巴插件"
    echo "10. 安装土块插件"
    echo "11. 安装ffmpeg"
    echo "12. 退出"
    echo -e "\\e[34m ---------------------------------- \\e[0m"
    read -p "请输入你的选择：" option
    case $option in
    1)
    bash <(curl -sL https://gitee.com/baihu433/Ubuntu-Yunzai/raw/master/plug-in.sh)
    ;;
    2)
    echo "开始安装插件"
    echo "喵喵插件，逍遥插件，锅巴插件，抽卡插件，土块插件，千羽插件(仅Yunzai-Bot可用)，椰奶插件，以及云溪院ffmpeg"
    if [ -d "$jh/miao-plugin" ]; then
        echo "已安装喵喵"
        else
            git clone --depth=1 https://gitee.com/yoimiya-kokomi/miao-plugin.git ./plugins/miao-plugin && pnpm install image-size -w
        fi
    if [ -d "$jh/xiaoyao-cvs-plugin" ]; then
        echo "已安装逍遥插件"
        else
            git clone --depth=1 https://gitee.com/Ctrlcvs/xiaoyao-cvs-plugin.git ./plugins/xiaoyao-cvs-plugin
    fi
    if [ -d "$jh/flower-plugin" ]; then
        echo "已安装抽卡插件"
        else
            git clone --depth=1 https://gitee.com/Nwflower/flower-plugin.git ./plugins/flower-plugin
    fi
    if [ ! -d "$jh/qianyu-plugin" ] && [ -d "/root/Yunzai-Bot" ]; then
        git clone --depth=1 https://gitee.com/think-first-sxs/qianyu-plugin.git ./plugins/qianyu-plugin/
        else
            echo "您已安装千羽插件或喵崽不支持安装千羽插件"
    fi
    if [ -d "$jh/yenai-plugin" ]; then
        echo "已安装椰奶插件"
        else
            git clone --depth=1 https://gitee.com/yeyang52/yenai-plugin.git ./plugins/yenai-plugin
            pnpm add systeminformation cheerio -w
    fi
    if [ -d "$jh/guoba-plugin" ]; then
        echo "已安装锅巴插件"
        else
            git clone --depth=1 https://gitee.com/guoba-yunzai/guoba-plugin.git ./plugins/guoba-plugin
            pnpm install --filter=guoba-plugin
    fi
    if [ -d "$jh/earth-k-plugin" ]; then
        echo "已安装土块插件"
        else
            git clone --depth=1 https://gitee.com/SmallK111407/earth-k-plugin.git ./plugins/earth-k-plugin
    fi
    cd
    if ! command -v ffmpeg &> /dev/null; then
         echo "ffmpeg 未安装，开始安装ffmpeg。"
         cd $HOME         
         wget -c https://gitee.com/yunxiyuan/yxybot/releases/download/ffmpeg/ffmpeg-arm64
         mv /root/ffmpeg-arm64 /usr/local/bin/ffmpeg
         chmod 777 /usr/local/bin/ffmpeg
         ln -l /usr/local/bin/ffmpeg
         else
             ffmpeg_version=$(ffmpeg -version | grep "ffmpeg version" | awk '{print $3}')
             required_version="4.0"
             if [[ "$(printf '%s\n' "$required_version" "$ffmpeg_version" | sort -V | head -n1)" != "$required_version" ]]; then
                 echo "Error: ffmpeg 版本号太低，请升级到4.0以上。"
                 exit 1
             fi
             echo "ffmpeg 自检通过。"
             echo "已安装ffmpeg"
    fi
    ;;
    3)
    apt-get update
    apt-get upgrade -y
    apt-get install python3 -y
    apt-get install python3-pip -y
    apt install python3.10-venv -y
    echo "pip，Python安装成功"
    python3.10 -m pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
    python3.10 -m pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
    python3.10 -m pip install poetry
    echo "poetry安装成功"
    echo "开始安装py-plugin"
    cd $lsy/plugins && git clone --depth=1 https://gitee.com/realhuhu/py-plugin.git
    echo "开始安装py-plugin的依赖"
    cd py-plugin && pnpm install iconv-lite @grpc/grpc-js @grpc/proto-loader -w
    poetry install
    echo "py配置成功"
    ;;
    4)
    if [ -d "$jh/miao-plugin" ]; then
    echo "已安装喵喵"
    else
        git clone --depth=1 https://gitee.com/yoimiya-kokomi/miao-plugin.git ./plugins/miao-plugin && pnpm install image-size -w
    fi
    ;;
    5)
    if [ -d "$jh/xiaoyao-cvs-plugin" ]; then
        echo "已安装逍遥插件"
        else
            git clone --depth=1 https://gitee.com/Ctrlcvs/xiaoyao-cvs-plugin.git ./plugins/xiaoyao-cvs-plugin
    fi
    ;;
    6)
    if [ -d "$jh/flower-plugin" ]; then
        echo "已安装抽卡插件"
        else
            git clone --depth=1 https://gitee.com/Nwflower/flower-plugin.git ./plugins/flower-plugin
    fi
    ;;
    7)
    if [ ! -d "$jh/qianyu-plugin" ] && [ -d "/root/Yunzai-Bot" ]; then
        git clone --depth=1 https://gitee.com/think-first-sxs/qianyu-plugin.git ./plugins/qianyu-plugin/
        else
            echo "您已安装千羽插件或喵崽不支持安装千羽插件"
    fi
    ;;
    8)
    if [ -d "$jh/yenai-plugin" ]; then
        echo "已安装椰奶插件"
        else
            git clone --depth=1 https://gitee.com/yeyang52/yenai-plugin.git ./plugins/yenai-plugin
            pnpm add systeminformation cheerio -w
    fi
    ;;
    9)
    if [ -d "$jh/guoba-plugin" ]; then
        echo "已安装锅巴插件"
        else
            git clone --depth=1 https://gitee.com/guoba-yunzai/guoba-plugin.git ./plugins/guoba-plugin
            pnpm install --filter=guoba-plugin
    fi
    ;;
    10)
    if [ -d "$jh/earth-k-plugin" ]; then
        echo "已安装土块插件"
        else
            git clone --depth=1 https://gitee.com/SmallK111407/earth-k-plugin.git ./plugins/earth-k-plugin
    fi
    ;;
    11)
     if ! command -v ffmpeg &> /dev/null; then
         echo "ffmpeg 未安装，开始安装ffmpeg。"
         cd $HOME         
         wget -c https://gitee.com/yunxiyuan/yxybot/releases/download/ffmpeg/ffmpeg-arm64
         mv /root/ffmpeg-arm64 /usr/local/bin/ffmpeg
         chmod 777 /usr/local/bin/ffmpeg
         ln -l /usr/local/bin/ffmpeg
         else
             ffmpeg_version=$(ffmpeg -version | grep "ffmpeg version" | awk '{print $3}')
             required_version="4.0"
             if [[ "$(printf '%s\n' "$required_version" "$ffmpeg_version" | sort -V | head -n1)" != "$required_version" ]]; then
                 echo "Error: ffmpeg 版本号太低，请升级到4.0以上。"
                 exit 1
             fi
             echo "ffmpeg 自检通过。"
             echo "已安装ffmpeg"
    fi
    ;;
    *)
    echo "输入有误，请重新输入"
    ;;
    esac
    else
        echo "请先安装云崽机器人"
fi
